package com.mujin.keji.collectionsystem.interceptor;

import android.text.TextUtils;
import android.util.Log;

import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.SPUtil;
import com.orhanobut.logger.Logger;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class SeesionInterceptor implements Interceptor {


    private static final String TAG = "SeesionInterceptor";
    private String sessionId;

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        Response response = chain.proceed(original);
        sessionId = SPUtil.getData(Constant.SP.SP, Constant.SP.SESSION_ID, String.class, null);
        if (TextUtils.isEmpty(sessionId)) {
            if (EmptyUtils.isNotEmpty(response) && EmptyUtils.isNotEmpty(response.headers()) && EmptyUtils.isNotEmpty(response.headers().value(1))) {
                sessionId = response.headers().value(1).split(";")[0].split("=")[1];
                SPUtil.putData(Constant.SP.SP, Constant.SP.SESSION_ID, sessionId);
            }
        }
        Request.Builder builder = original.newBuilder();
        builder.addHeader("Cookie", sessionId);
        return chain.proceed(builder.build());
    }


}